<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <#include "/include/head_common.ftl">
    <title>${title}</title>
    <script type="text/javascript">
      function reload() {
          $("#change_sql_key").val($("#defineKey").val());
          $("#changeForm").submit();
          return false;
      }
      function reloadSqlConditionSize(){
        $.cookie('sql.query.condition.row.size',$("#reloadConditionSize").val(),{expires:10000,path:"/"});
        history.go(0);
      }
      function detail(index,head,json){
        $("#detail_index").val(index);
        $("#detail_head").val(head);
        $("#detail_json").val(json);
        $("#detailForm").submit();
        return false;
      }
      //导出功能
      function exportExcel(){
        $("#searchForm")[0].reset();
        var params = $("#searchForm").serialize();
        var downloadWindow=window.open('_blank');
        downloadWindow.location='${rootUrl}/sql/export?<#if staticKey>defineKey=${defineKey}&</#if>'+params ;
      }
      //查询功能，页码重置为1
      function queryData(){
          <#if page??>
            <#list sqlParams as sqlParam>
                <#if sqlParam.pageNum?? && sqlParam.pageNum >
                    $("#sql${sqlParam.paramName}").val(1);
                </#if>
            </#list>
          </#if>
          $("#searchForm").submit();
      }

      //带分页的功能
      <#if page?? && page.pages gt 0>
        function page(n,s){
            $("#searchForm")[0].reset()
            <#list sqlParams as sqlParam>
                <#if sqlParam.pageNum?? && sqlParam.pageNum >
                    $("#sql${sqlParam.paramName}").val(n);
                </#if>
                <#if sqlParam.pageSize?? && sqlParam.pageSize >
                    $("#sql${sqlParam.paramName}").val(s);
                </#if>
            </#list>
            $("#searchForm").submit();
            return false;
        }
        function selectPageSizeQueryData(){
            $("#searchForm")[0].reset()
            <#list sqlParams as sqlParam>
                <#if sqlParam.pageNum?? && sqlParam.pageNum >
                    $("#sql${sqlParam.paramName}").val(1);
                </#if>
                <#if sqlParam.pageSize?? && sqlParam.pageSize >
                    $("#sql${sqlParam.paramName}").val($('#selectPageSize').val());
                </#if>
            </#list>
            $("#searchForm").submit();
            return false;
        }
        function selectPageNumQueryData(){
            $("#searchForm")[0].reset();
            <#list sqlParams as sqlParam>
                <#if sqlParam.pageNum?? && sqlParam.pageNum >
                    $("#sql${sqlParam.paramName}").val($('#selectPageNum').val());
                </#if>
            </#list>
            $("#searchForm").submit();
            return false;
        }
      </#if>

      $(document).ready(
			function() {
                closeTip();
				$("#searchForm").validate({
				    submitHandler : function(form) {
					    $("#btnSubmit").attr("disabled","disabled");
					    $("#btnExport").attr("disabled","disabled");
					    $("#btnRest").attr("disabled","disabled");
					    loading('正在查询，请稍等...');
						form.submit();
					},
				    rules:{
				        "defineKey":{required: true}
                    },
                    messages: {
                        defineKey: "请选择查询项目"
                    }
				});
				<#list sqlParams as sqlParam>
				    <#if sqlParam.paramType = 'java.util.Date'>
		                $("#sql${sqlParam.paramName}").click(function () {
					        WdatePicker({
						        name:'zh-cn',
						        charset:'UTF-8',
						        dateFmt: '${sqlParam.format!''}',
						        isShowClear: <#if sqlParam.required?? && sqlParam.required > false <#else> true</#if>,
						        readOnly: false
					        })
			        	});
				    </#if>
                </#list>
                <#if autoWebLoad = true>
                    $("#searchForm").submit();
                </#if>
			}
	  );
    </script>
</head>
<body>
<#if !staticKey >
<ul class="nav nav-tabs">
    <li class="active"><a href="${rootUrl}/sql/query">sql查询</a></li>
    <li><a href="${rootUrl}/sql/custom">自定义查询</a></li>
</ul>
</#if>
<form id="changeForm" action="${rootUrl}/sql/query" style="display: none"
      method="post" class="breadcrumb form-search form-signin">
    <input type="hidden" id="sql_select" name="sql_type" value="change">
    <input type="hidden" id="change_sql_key" name="defineKey" value="${defineKey!''}"/>
</form>

<form id="detailForm" action="${rootUrl}/sql/detail" style="display: none"
      method="post" class="breadcrumb form-search form-signin">
    <input type="hidden" id="detail_index" name="index" value="">
    <input type="hidden" id="detail_head" name="head" value="">
    <input type="hidden" id="detail_json" name="json" value="">
</form>

<div align="center">
<form id="searchForm" action="${rootUrl}/sql/query<#if staticKey>?defineKey=${defineKey}</#if>"
      method="post" class="breadcrumb form-search form-signin">
    <#assign oneRow = fm.cookieValue('sql.query.condition.row.size',fm.getSystemParameter('sql_query_condition_row_size',4))?number />
    <#assign showCondition = 0 >
    <table id="contentTable"
       class="table-striped  table-condensed">
        <input type="hidden" id="sql_select" name="sql_type" value="select">
        <input type="hidden" id="staticKey" name="staticKey" value="<#if staticKey>true<#else>false</#if>">
        <tr>
            <td style="text-align: right"><label>配置SQL:</label></td>
            <td>
                <#if staticKey>
                    <a href="${rootUrl}/sql/query?defineKey=${defineKey}" target="_blank"><input type="text" id ="defineKeyDesc" name="defineKeyDesc" value="${title}" disabled/></a>
                <#else>
                    <select id="defineKey" name="defineKey" class="input-large" onchange="reload()" <#if staticKey>disabled</#if> >
                        <option value="">请选择</option>
                        <#list sqls as sql>
                            <option value="${sql.defineKey}" <#if sql.defineKey = defineKey!''> selected </#if>>${sql.defineKey!''}(${sql.sqlDescribe!''})</option>
                        </#list>
                    </select>
                </#if>
            </td>
        <#assign index = 1 />
        <#list sqlParams as sqlParam>
            <#if sqlParam.pageNum || sqlParam.pageSize>
                <input  id="sql${sqlParam.paramName}" type="hidden" name="sql.${sqlParam.paramName}"  value="${sqlParam.defaultValue}"/>
            <#else>
                <#if index = 0><tr></#if>
                <#assign index = index + 1 />
                <#assign showCondition = showCondition + 1 />
                <td style="text-align: right"><label>${sqlParam.paramDescribe}:</label></td>
                <td>
                    <#if sqlParam.paramType = 'java.lang.Boolean'>
                        <label><input  name="sql.${sqlParam.paramName}" type="radio" value="true"
                                   <#if sqlParam.defaultValue?? && sqlParam.defaultValue = 'true'>checked="checked"</#if> />是</label>
                        <label><input  name="sql.${sqlParam.paramName}" type="radio" value="false"
                                   <#if sqlParam.defaultValue?? && sqlParam.defaultValue = 'false'>checked="checked"</#if>/>否</label>
                    <#elseif sqlParam.dropDownB?? && sqlParam.dropDownB>
                        <select id="sql${sqlParam.paramName}" name="sql.${sqlParam.paramName}" class="input-large <#if sqlParam.required?? && sqlParam.required> required </#if>">
                            <option value= "">请选择</option>
                            <#list sqlParam.dropDownMap?keys as key>
                            <option value="${key}" <#if sqlParam.defaultValue = key!''> selected </#if>>${sqlParam.dropDownMap[key]}</option>
                            </#list>
                        </select>
                    <#else>
                        <input id="sql${sqlParam.paramName}" type="text" name="sql.${sqlParam.paramName}" maxlength="100"
                            class="input-large
                            <!-- 样式和校验的挨个填充 -->
                            <!-- 日期 -->
                            <#if sqlParam.paramType = 'java.util.Date'> Wdate-datetime date </#if>
                            <!-- 整形-->
                            <#if sqlParam.paramType = 'java.lang.Integer'> digits </#if>
                            <#if sqlParam.paramType = 'java.lang.Long'> digits </#if>
                            <#if sqlParam.paramType = 'java.math.BigInteger'> digits </#if>
                            <!-- 浮点数 -->
                            <#if sqlParam.paramType = 'java.lang.Double'> number </#if>
                            <#if sqlParam.paramType = 'java.lang.Float'> number </#if>
                            <#if sqlParam.paramType = 'java.math.BigDecimal'> number </#if>
                            <!-- 是否必须-->
                            <#if sqlParam.required?? && sqlParam.required> required </#if>
                            "
                            <#if sqlParam.paramType = 'java.util.Date'> autocomplete="off"  </#if>
                           value="${sqlParam.defaultValue}"/>
                    </#if>
                    &nbsp;
                </td>
                <#if index = oneRow>
                <#assign index = 0 /> </tr>
                </#if>
            </#if>
        </#list>
        <#if index ==0>
            <tr align="center" >
        </#if>
        <td colspan = ${(oneRow-index)*2} align="center">
            <input id="btnSubmit" class="btn btn-primary" type="button" value="查询" onclick="queryData()"/>
            &nbsp;
            <input id="btnRest" class="btn" type="reset" value="重置"/>
            &nbsp;
            <input id="btnExport" class="btn btn-primary "
                   type="button" value="导出" <#if (tbody?size > 0)> onclick="return confirmx('确认要导出吗？', function(){exportExcel()})" </#if>/>
            &nbsp;
            <select id = "reloadConditionSize" style="width: auto" onchange="reloadSqlConditionSize()"
                <#if oneRow gt showCondition + 1> disabled</#if> >
                <option value="2" <#if oneRow = 2> selected </#if>>2</option>
                <option value="3" <#if oneRow = 3> selected </#if>>3</option>
                <option value="4" <#if oneRow = 4> selected </#if>>4</option>
                <option value="5" <#if oneRow = 5> selected </#if>>5</option>
                <option value="6" <#if oneRow = 6> selected </#if>>6</option>
            </select>
        </td>
        </tr>
    </table>
</form>
</div>
<#include "/include/message.ftl">
<#if !message??>
    <#if httpMethod = "POST" >
        <div align="center" style="color: red; height: 34px; font-size:15px">
            <#if page?? && page.pages gt 0>
                <strong>当前第</strong>
                <#if (page.pages > 1) >
                    &nbsp;
                    <select id = "selectPageNum" style="width:60px" onchange="selectPageNumQueryData()">
                        <#list 1..page.pages as i>
                            <option value="${i}"  <#if page.pageNum = i> selected </#if>>${i}</option>
                        </#list>
                    </select>
                    &nbsp;
                <#else>
                    <strong>${page.pageNum}</strong>
                </#if>
                <strong>页,&nbsp;共${page.pages}页,&nbsp;&nbsp;每页</strong>
                &nbsp;
                <select id="selectPageSize" style="width:60px;" onchange="selectPageSizeQueryData()">
                    <option value="3" <#if page.pageSize = 3> selected </#if>> 3</option>
                    <option value="4" <#if page.pageSize = 4> selected </#if>> 4</option>
                    <option value="5" <#if page.pageSize = 5> selected </#if>> 5</option>
                    <option value="6" <#if page.pageSize = 6> selected </#if>> 6</option>
                    <option value="7" <#if page.pageSize = 7> selected </#if>> 7</option>
                    <option value="8" <#if page.pageSize = 8> selected </#if>> 8</option>
                    <option value="9" <#if page.pageSize = 9> selected </#if>> 9</option>
                    <option value="10" <#if page.pageSize = 10> selected </#if>>10</option>
                    <option value="15" <#if page.pageSize = 15> selected </#if>>15</option>
                    <option value="20" <#if page.pageSize = 20> selected </#if>>20</option>
                    <option value="25" <#if page.pageSize = 25> selected </#if>>25</option>
                    <option value="30" <#if page.pageSize = 30> selected </#if>>30</option>
                    <option value="35" <#if page.pageSize = 35> selected </#if>>35</option>
                    <option value="40" <#if page.pageSize = 40> selected </#if>>40</option>
                    <option value="45" <#if page.pageSize = 45> selected </#if>>45</option>
                    <option value="50" <#if page.pageSize = 50> selected </#if>>50</option>
                    <option value="100" <#if page.pageSize = 100> selected </#if>>100</option>
                    <option value="200" <#if page.pageSize = 200> selected </#if>>200</option>
                    <option value="500" <#if page.pageSize = 500> selected </#if>>500</option>
                    <option value="1000" <#if page.pageSize = 1000> selected </#if>>1000</option>
                </select>
                &nbsp;
                <strong>条,共${page.total}条</strong>
            <#else>
                <strong><#if (tbody?size > 0) >共&nbsp;${tbody?size}&nbsp;条 <#else> 无数据</#if></strong>
            </#if>
        </div>
    </#if>
    <div style="clear:both;"></div>
    <div style="overflow:auto">
        <table id="contentTable" class="table table-striped table-bordered table-condensed">
            <thead>
            <tr>
                <th <#if htmlCenterShow =true> style="text-align: center;" </#if> >序号</th>
                <#list thead as head>
                    <#if !hideColumns?seq_contains(head)>
                        <th <#if htmlCenterShow =true> style="text-align: center;" </#if> >${head}</th>
                    </#if>
                </#list>
            </tr>
            </thead>
            <tbody>
            <#assign index = 1 />
            <#list tbody as body>
                <tr>
                    <td <#if htmlCenterShow =true> style="text-align: center;" </#if> >${index}</td>
                    <#assign tdIndex = 0/>
                    <#list thead as head>
                        <#if tdIndex = 0 >
                            <#assign ahref= fm.urlEncode(body.json!'')/>
                            <td <#if htmlCenterShow =true> style="text-align: center;" </#if> >
                                <a onclick="detail(${index},'${theadSplit!''}','${body['json']!''}')">${body[head]!''} </a>
                            </td>
                        <#else>
                            <#if !hideColumns?seq_contains(head)>
                                <td <#if htmlCenterShow =true> style="text-align: center;" </#if> >${body[head]!''}</td>
                            </#if>
                        </#if>
                        <#assign tdIndex = tdIndex + 1/>
                    </#list>
                    <#assign index = index + 1 />
                </tr>
            </#list>
            </tbody>
        </table>
    </div>
</#if>
<#if page?? && page.pages gt 0>
    <div class="pagination">
        <#assign funName = 'page' />
        <#assign ftPage = page>
        <#include "/include/page.ftl">
    </div>
</#if>
</body>
<#include "/include/login_trace.ftl">
</html>